package com.example.fw;


public class ContactHelper extends HelpersBase {

	public ContactHelper(ApplicationManager applicationManager) {
	  super(applicationManager);
  }

	public void createContact(Contact contact) {
		initContactCreation();
		fillContactForm(contact);
		submitContactCreation();
		
  }

	public Contact getFirstContact() {
	  selectFirstContact();
	  initContactModification();
	  Contact contact = new Contact()
	  		.withFirstName(manager.getAutoItHelper().getText("TDBEdit12"))
	  		.withLastName(manager.getAutoItHelper().getText("TDBEdit11"));
	  cancelContactModification();
	  return contact;
  }

	public void deleteFirstContact() {
	  selectFirstContact();
	  initContactRemoval();
	  submitContactRemoval();
  }

	public boolean checkContactListEmpty() {
	  selectFirstContact();
	  initNotSelectedContactModfication();
	  
	  // check that window text is what it needs to be!!!
	  String windowText = manager.getAutoItHelper().getWindowHeader("Information");
	  if (windowText != "Information") {
	  	cancelContactModification();
	  	return false;
	  }
	  
	  cancelSelectItemDialog();
	  return true;
  }

	private void initContactCreation() {
	  manager.getAutoItHelper()
				.winWaitAndActivate("AddressBook Portable", "", 5000)
				.click("Add").winWaitAndActivate("Add Contact", "", 5000);
  }

	private void fillContactForm(Contact contact) {
	  manager.getAutoItHelper()
				.send("TDBEdit12", contact.getFirstName())
				.send("TDBEdit11", contact.getLastName());
  }

	private void submitContactCreation() {
	  manager.getAutoItHelper()
				.click("Save")
				.winWaitAndActivate("AddressBook Portable", "", 5000);
  }

	private void selectFirstContact() {
	  manager.getAutoItHelper()
				.winWaitAndActivate("AddressBook Portable", "", 5000)
				.click("TListView1")
				.send("{DOWN}{SPACE}");
  }

	private void initContactModification() {
	  manager.getAutoItHelper()
				.click("Edit")
				.winWaitAndActivate("Update Contact", "", 5000);
  }

	private void cancelContactModification() {
	  manager.getAutoItHelper()
	  		.click("Cancel")
	  		.winWaitAndActivate("AddressBook Portable", "", 5000);
  }

	private void initContactRemoval() {
	  manager.getAutoItHelper()
		.click("Delete")
		.winWaitAndActivate("Confirm", "", 5000);
  }

	private void submitContactRemoval() {
	  manager.getAutoItHelper()
//		.click("Yes")
	  .send("!y")
		.winWaitAndActivate("AddressBook Portable", "", 5000);
  }

	private void initNotSelectedContactModfication() {
	  manager.getAutoItHelper()
		.click("Edit")
		.winWaitAndActivate("Information", "", 5000);
  }

	private void cancelSelectItemDialog() {
	  manager.getAutoItHelper()
		.click("OK")
		.winWaitAndActivate("AddressBook Portable", "", 5000);
  }

}
